package com.example.demozmq.zmq;

import java.nio.charset.Charset;
import org.zeromq.ZMQ;

public class ZmqServer {

    public static void main(String[] args) {

        try {
            ZMQ.Context context = ZMQ.context(1);
            ZMQ.Socket socket = context.socket(ZMQ.REP);
            // 绑定端口
            socket.bind("tcp://*:5555");
            while (true) {
                // 等待接收消息
                byte[] request = socket.recv();
                String reqTest = new String(request);
                System.out.println("接收到的消息：" + reqTest);
                // 返回消息给客户端
                byte[] reply = (System.currentTimeMillis() + "你好啊，我是服务端。").getBytes(Charset.defaultCharset());
                socket.send(reply, 0);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
